#include "Log.h"

Log::Log(Bpchar path)
{
    LogFilePath = path;
    Handle = NULL;
    memset(Content,0,LOG_CONTENT_LENGTH);
}

Log::~Log()
{
    if(Handle)
        CloseLog();
}

BPcode Log::OpenLog()
{
    if(Handle)
        CloseLog();

    Handle = fopen(LogFilePath.c_str(),"a");

    return BP_OK;
}

BPcode Log::CloseLog()
{
    if(Handle)
    {
        fclose(Handle);
        Handle = NULL;
    }

    return BP_OK;
}

BPcode Log::Writeln(uint32_t level, char* ctt,...)
{
    int ret;

    if(!Handle)
        ret = OpenLog();

    if(ret == BP_OK)
    {
        va_list args;
        va_start(args,ctt);
        vfprintf(Handle,ctt,args);
        fprintf(Handle,"\n");
        va_end(args);

        CloseLog();

        return BP_OK;
    }
    else
        return BP_BAD;
}

BPcode Log::ReadLog()
{
    if(Handle)
        fgets(Content,LOG_CONTENT_LENGTH,Handle);

    return BP_OK;
}

Bpchar Log::GetLogContentPtr()
{
    return Content;
}
